<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Authentication</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongo.connecting.ssl.html">« Connecting over SSL</a></li>
      <li style="float: right;"><a href="mongo.connecting.rs.html">Replica Sets »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.connecting.html">Connecting</a></li>
    <li>Authentication</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongo.connecting.auth" class="section">
  <h2 class="title">Authentication</h2>
  <p class="para">
   If MongoDB is started with the <code class="literal">--auth</code> or
   <code class="literal">--keyFile</code> options, you must authenticate before you can do
   any operations with the driver. You may authenticate a connection by
   specifying the username and password in either the connection URI or the
   <code class="literal">&quot;username&quot;</code> and <code class="literal">&quot;password&quot;</code> options for
   <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span>.
  </p>
  <div class="example" id="mongo.connecting.auth-example">
   <p><strong>Example #1 Authenticating against the &quot;admin&quot; database</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"username"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   By default, the driver will authenticate against the <code class="literal">admin</code>
   database. You may authenticate against a different database by specifying it
   in either the connection URI or the <code class="literal">&quot;db&quot;</code> option for
   <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span>.
  </p>
  <div class="example" id="mongo.connecting.auth-db-example">
   <p><strong>Example #2 Authenticating against normal databases</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost/myDatabase"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"db"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"myDatabase"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   If your connection is dropped, the driver will automatically attempt to
   reconnect and reauthenticate you.
  </p>
 </div></div></div></body></html>